import { useEventListener } from "@vueuse/core";
import { useTabStore, useThemeStore } from "@/store";

/** 全局事件 */
export const useGlobalEvents = () => {
  const themeStore = useThemeStore();
  const tabStore = useTabStore();
  const scope = effectScope();

  /** 页面离开时缓存多页签数据 */
  useEventListener(window, "beforeunload", () => {
    themeStore.cacheThemeSettings();
    tabStore.cacheTabRoutes();
  });

  onScopeDispose(() => {
    scope.stop();
  });
};
